from io import BytesIO

from django.http import HttpResponse
from django.shortcuts import render

from saas_main.forms import user_forms
from utils.verify_code_pic import verify_code


def register(request):
    form = user_forms.RegisterForms()
    return render(request, 'register.html', {"form": form})


def login(request):
    pre_page = request.GET.get('pre')
    form = user_forms.LoginForms()
    return render(request, 'login.html', {"form": form, 'pre_page': pre_page})


def login_code(request):
    img, code = verify_code()
    print(code)
    request.session['image_code'] = code
    request.session.set_expiry(60)
    steam = BytesIO()
    img.save(steam, 'png')
    return HttpResponse(steam.getvalue())
